package ldh.im.web.controller;

import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.web.constant.LogType;
import ldh.im.web.pojo.Log;
import ldh.im.web.pojo.User;
import ldh.im.web.pojo.where.LogWhere;
import ldh.im.web.service.LogService;
import ldh.im.web.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author: ldh
* @date: 2019-10-25 18:54:00
*/
@Controller
@RequestMapping("log")
public class LogController extends BaseController {

	@Resource(name = "logService")
	private LogService logService;
	@Resource(name = "userService")
	private UserService userService;

	/**
	 * 字符串转化成时间
	 */
	@InitBinder
	protected void initBinder(HttpServletRequest request,
                              ServletRequestDataBinder binder) throws Exception {
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	}

	@RequiresPermissions("log:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Log log = logService.getById(id);
    	if (log.getUser() != null) {
    		User user = userService.getById(log.getUser().getId());
    		log.setUser(user);
		}
    	model.addAttribute("log", log);
		model.addAttribute("active", "log");
        return "log/logView";
    }

	@RequiresPermissions("log:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Log log = logService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", log)
				.toJson();
    }

	@RequiresPermissions("log:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute LogWhere logWhere, Model model, HttpSession httpSession) {
		PageResult<Log> logs = logService.findByLogWhere(logWhere);
		List<Long> userIds = logs.getBeans().stream().filter(log->log.getUser() != null).map(log->log.getUser().getId()).collect(Collectors.toList());
		List<User> userList = userService.getByIds(userIds);
		Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user->user));
		logs.getBeans().stream().filter(log->log.getUser() != null).forEach(log->{
			User user = userMap.get(log.getUser().getId());
			log.setUser(user);
		});
		model.addAttribute("logs", logs);
		model.addAttribute("active", "log");
		model.addAttribute("log", logWhere);
		model.addAttribute("logTypes", LogType.values());
		model.addAttribute("tenants", getAllTenants());
		return "log/logList";
	}

	@RequiresPermissions("log:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute LogWhere logWhere, Model model, HttpSession httpSession) {
		PageResult<Log> logs = logService.findByLogWhere(logWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", logs)
				.toJson();
	}
	
	@RequiresPermissions("log:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		Log log = logService.getById(id);
        Objects.requireNonNull(log);
		logService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}